//
//  GMArtworkItem.m
//  gentleman
//
//  Created by 范月成 on 2017/12/8.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "GMArtworkItem.h"
#import "VendorHeader.h"

@implementation GMArtworkItem

+ (NSString *)dbName {
    return @"GMGentleman_Database";
}
+ (NSString *)tableName {
    return @"GMArtworkItem_Table";
}
+ (NSString *)primaryKey {
    return @"imagePageUrl";
}
+ (NSArray *)persistentProperties {
    static NSArray *properties = nil;
    if (!properties) {
        properties = @[
                       @"gid",
                       @"imagePageUrl",
                       @"imageUrl",
                       @"pageIndex"
                       ];
    };
    return properties;
}

+ (void)getArtworksWithUrl:(NSString *)url
                       gid:(NSInteger)gid
                   showkey:(NSString *)showkey
                 totalPage:(NSInteger)page
                 completed:(getArtworkItemsCompleted)completed {
 
    NSInteger total = page % 40 == 0 ? page / 40 : page / 40 + 1;
    NSMutableArray *artworks = [NSMutableArray array];
    for (NSInteger i = 0; i < total; i++) {
        [self getArtWithUrl:url turn:i completed:^(NSArray *imgPages) {
            [artworks addObjectsFromArray:imgPages];
            if (artworks.count==page) {
                NSArray *resultArray = [artworks sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
                    
                    GMArtworkItem *item1 = obj1;
                    GMArtworkItem *item2 = obj2;
                    
                    if (item1.pageIndex > item2.pageIndex) {
                        return NSOrderedDescending;//降序
                    } else if (item1.pageIndex < item2.pageIndex){
                        return NSOrderedAscending;//升序
                    } else {
                        return NSOrderedSame;//相等
                    }
                    
                }];
                
                !completed?:completed(resultArray.mutableCopy);
            }
        }];
    }
}

+ (void)getArtWithUrl:(NSString *)url
                 turn:(NSInteger)turn
            completed:(void (^)(NSArray *imgPages))completed {
    NSString *finalUrl = @"";
    if (turn == 0) {
        finalUrl = [NSString stringWithFormat:@"%@?inline_set=ts_m",url];
    } else {
        finalUrl = [NSString stringWithFormat:@"%@?inline_set=ts_m&p=%ld",url,(long)turn];
    }

    [XMCenter sendRequest:^(XMRequest * _Nonnull request) {
        request.httpMethod = kXMHTTPMethodGET;
        request.url = finalUrl;
        request.responseSerializerType = kXMResponseSerializerRAW;
    } onSuccess:^(id  _Nullable responseObject) {
        TFHpple *parser = [[TFHpple alloc] initWithHTMLData:responseObject];
        NSArray *pageURL  = [parser searchWithXPathQuery:@"//div [@class='gdtm']//a"];
        
        NSMutableArray *imgPages = [NSMutableArray array];
        for (NSInteger i = 0; i < pageURL.count; i++) {
            TFHppleElement *e = pageURL[i];
            NSInteger index = i + turn * 40;
            NSString *url = e.attributes[@"href"];
            
            GMArtworkItem *item = [[GMArtworkItem alloc] init];
            item.pageIndex = index;
            item.imagePageUrl = url;
            [imgPages addObject:item];
        }
        !completed?:completed(imgPages);
    } onFailure:^(NSError * _Nullable error) {
        !completed?:completed(nil);
    }];
}
 

@end
